//
//  RouteViewController.m
//  课余时间
//
//  Created by zgr on 16/4/11.
//  Copyright © 2016年 张大少. All rights reserved.
//

#import "RouteViewController.h"
#import "RequestManager.h"
#import "RouteModel.h"
#import "RouteTableViewCell.h"
#import "UITableView+SDAutoTableViewCellHeight.h"
#import "UIView+SDAutoLayout.h"
@interface RouteViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic, strong)UITableView *tableView;
@property (nonatomic,strong)NSMutableArray *modelArray;
@end

@implementation RouteViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _modelArray = [NSMutableArray array];
    _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, KWIDTH, KHEIGHT - 64) style:UITableViewStylePlain];
    _tableView.dataSource = self;
    _tableView.delegate = self;
    [_tableView registerClass:[RouteTableViewCell class] forCellReuseIdentifier:@"cell"];
    [self.view addSubview:_tableView];
    
    [self requestUrlWithUrlstring:[NSString stringWithFormat:@"http://chanyouji.com/api/destinations/plans/%@.json?page=1",_descID]];
    
    
    // Do any additional setup after loading the view from its nib.
}
-(void)requestUrlWithUrlstring:(NSString *)urlString
{
    [[[RequestManager alloc]init] requestWithUrl:urlString requestType:RequestTypeGet parDic:nil finish:^(NSData *data) {
        
        NSArray *bigArray = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
        NSLog(@"%@",bigArray);
        for (NSDictionary *dic in bigArray) {
            
            RouteModel *model = [[RouteModel alloc]init];
            [model setValuesForKeysWithDictionary:dic];
            [_modelArray addObject:model];
        }
        dispatch_async(dispatch_get_main_queue(), ^{
            [_tableView reloadData];
        });
    } error:^(NSError *error) {
        
    }];
    
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

{
    return 1;
    
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _modelArray.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    RouteTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    cell.model = _modelArray[indexPath.row];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    Class currentClass = [RouteTableViewCell class];
    RouteModel *model = _modelArray[indexPath.row];
    // model = model;
    //  NSLog(@"%@====%ld -----%ld",model.title,indexPath.section,indexPath.row);
    return  [self.tableView cellHeightForIndexPath:indexPath model:model keyPath:@"model" cellClass:currentClass contentViewWidth:[self cellContentWith]];
}
-(CGFloat)cellContentWith
{
    CGFloat width = [UIScreen mainScreen].bounds.size.width;
    
    // 适配ios7
    if ([UIApplication sharedApplication].statusBarOrientation != UIInterfaceOrientationPortrait && [[UIDevice currentDevice].systemVersion floatValue] < 8) {
        width = [UIScreen mainScreen].bounds.size.height;
    }
    return width;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
